//
//  PlaceSearchServices.swift
//  placepic
//
//  Created by elesahich on 2020/07/12.
//  Copyright © 2020 elesahich. All rights reserved.
//

import Foundation
import Alamofire

struct PlaceSearchServices {
    
    static let placeSearchServices = PlaceSearchServices()
    func getplaceSearchList(_ group: Int, _ queryString: String, completion: @escaping (DataClass?) -> Void) {
        
        guard let token = UserDefaults.standard.string(forKey: "accessToken") else { return }
        let urlString = HomeAPIs.placeSearchURL + "\(group)" + "?query=" + "\(queryString)"
        let encodedString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
        let url = URL(string: encodedString)!
        
        let header: HTTPHeaders = [
            "Content-Type": "application/json",
            "token": token,
        ]
        
        Alamofire.request(url, method: .get,
                          parameters: .none,
                          encoding: JSONEncoding.default,
                          headers: header)
            .responseJSON { response in
                
                switch response.result {
                case .success:
                    guard let data = response.data else { return }
                    do {
                        let decoder = JSONDecoder()
                        let object = try decoder.decode(TempResponseResult<DataClass>.self, from: data)
                        if object.success {
                            completion(object.data)
                        } else {
                            completion(nil)
                        }
                    } catch (let err) {
                        print(err.localizedDescription)
                    }
                case .failure:
                    completion(nil)
                }
        }
        
    }
}

